<template>
  <div class="components-container">
    <!-- 弹窗  -->
    <sweet-modal ref="dialog" class="dialog">
      <h3>{{title}}</h3>
      <div style=" width:500px; margin:30px 50px">
        <div class="layui-form-item">
          <label class="layui-form-label">名称</label>
          <div class="layui-input-block">
            <el-input v-model="memName" placeholder="请输入追悼厅名称"></el-input>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">类别</label>
          <div class="layui-input-block">
            <el-select v-model="memCodName" placeholder="请选择追悼厅" style="width:100%">
              <el-option v-for="item in memTypes" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </div>
        </div>
      </div>
      <div class="layui-form-item btnCenter">
        <el-button @click="closeDialog">取消</el-button>
        <el-button type="primary" :loading="loading" @click="submit">提交</el-button>
      </div>
    </sweet-modal>
    <sweet-modal ref="dialogInfo" class="dialog">
      <h3>{{title}}</h3>
      <div style=" width:500px; margin:30px 50px">
        <div class="layui-form-item">
          <label class="layui-form-label">名称</label>
          <div class="layui-input-block">
            <el-input v-model="memName" readonly></el-input>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layui-form-label">类别</label>
          <div class="layui-input-block">
            <el-input v-model="memCodnameDesc" readonly></el-input>
          </div>
        </div>
      </div>
      <div class="layui-form-item btnCenter">
        <el-button @click="closeDialog">取消</el-button>
      </div>
    </sweet-modal>
  </div>
</template>
<style rel="stylesheet/scss" lang="scss" scoped>
  @import "src/styles/common.scss";
  .btnCenter {
    text-align: center;
  }
</style>
<script>
  import sweetModal from "@/components/sweetModal";
  import apiBase from "@/api/apiBase.js";
  const ADD = "新增";
  const MODIFY = "修改";
  const DELETE = "删除";
  const INFO = "详情";
  export default {
    name: "MemorialMain",
    data() {
      return {
        loading: false,
        memTypes: [],
        title: null,
        order: null,
        memName: null,
        memCodName: null,
        memCodnameDesc:null
      };
    },
    methods: {
      //功能操作
      open(order, id) {
        this.order = order;
        this.dataRepeat();
        switch (this.order) {
          case ADD:
            this.title = "新增";
            this.initSelectData();
            this.$refs.dialog.open();
            break;
          case MODIFY:
            this.title = "修改";
            this.initSelectData();
            this.dataInfo(id);
            this.$refs.dialog.open();
            break;
          case INFO:
            this.title = "详情";
            this.dataInfo(id);
            this.$refs.dialogInfo.open();
            break;
          case DELETE:
            this.dataDelete(id);
            break;
          default:
        }
      },
      //提交
      submit() {
        switch (this.order) {
          case ADD:
            this.dataAdd();
            break;
          case MODIFY:
            this.dataModify();
            break;
          case DELETE:
            break;
          case INFO:
            this.$refs.dialogInfo.close();
            break;
          default:
        }
      },
      //获取追悼厅类型
      getMemType() {
        apiBase.DoService({},
          apiBase.getApiAdress().Code.GetMemorialType,
          res => {
            let memTypeData = res.data.content,
              legendsMemData = memTypeData.map(item => ({
                label: item.CodeName,
                value: item.CodeValue
              }));
            this.memTypes = legendsMemData;
          }
        );
      },
      //新增提交
      dataAdd() {
        apiBase.DoService({
            MEMNAME: this.memName,
            MEMCODNAME: this.memCodName,
            MEMSTATUS: this.memStatus
          },
          apiBase.getApiAdress().Memorial.Create,
          res => {
            this.$refs.dialog.close();
            this.$message({
              type: "success",
              message: "新增成功!"
            });
            this.loading = false;
            this.refreshData();
          },
          res => {
            this.$message.error(res.data.msg);
            this.loading = false;
          }
        );
      },
      //详情
      dataInfo(id) {
        this.memID = id;
        var data = {
          MEMID: id
        };
        apiBase.DoService(data, apiBase.getApiAdress().Memorial.Detail, res => {
          let row = res.data.content;
          this.memID = id;
          this.memName = row.MEMNAME;
          this.memCodName = row.MEMCODNAME;
          this.memStatus = row.MEMSTATUS;
          this.memCodnameDesc = row.MEMCODNAMEDESC
        });
      },
      //修改提交
      dataModify() {
        var data = {
          MEMID: this.memID,
          MEMNAME: this.memName,
          MEMCODNAME: this.memCodName,
          MEMSTATUS: this.memStatus
        };
        apiBase.DoService(
          data,
          apiBase.getApiAdress().Memorial.Modify,
          res => {
            this.$refs.dialog.close();
            this.$message({
              type: "success",
              message: "修改成功!"
            });
            this.refreshData();
          },
          res => {
            this.$message.error(res.data.msg);
          }
        );
      },
      // 删除表格列
      dataDelete(id) {
        var data = {
          MEMID: id
        };
        this.$confirm("此操作将永久删除该悼念厅信息, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(callback => {
          apiBase.DoService(data, apiBase.getApiAdress().Memorial.Delete, res => {
            this.refreshData();
            this.$message({
              message: "删除悼念厅成功！",
              type: "success"
            });
          });
        });
      },
      //数据重置
      dataRepeat() {
        this.memID = null;
        this.memName = null;
        this.memCodName = null;
        this.carPopedom = null;
        this.carStartDate = null;
        this.carIDEntificationCode = null;
        this.carBrand = null;
      },
      refreshData() {
        this.$emit("refreshData");
      },
      closeDialog() {
        if (this.order === ADD || this.order === MODIFY)
          this.$refs.dialog.close();
        else if (this.order === INFO) this.$refs.dialogInfo.close();
      },
      initSelectData() {
        this.getMemType();
      }
    },
    mounted() {},
    components: {
      sweetModal
    },
    created() {}
  };
</script>